﻿#region Using Directives

using System;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.IO;
using EnvDTE;
using FeatureBuilder.ContribLib.Helpers;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.TeamArchitect.PowerTools.Features;

#endregion

namespace FeatureBuilder.ContribLib.Commands
{
    /// <summary>
    /// Feature Extension Command that deletes a ProjectItem from a Project.
    /// </summary>
    [DisplayName("Delete ProjectItem")]
    [Description("Deletes a ProjectItem from a Project.")]
    [Category("IDE")]
    public class DeleteProjectItem : FeatureCommand
    {
        public ProjectItems ProjectItems { get; set; }
        public string Kind { get; set; }

        /// <summary>
        /// Gets the ServiceProvider that we will use to get a reference to DTE.
        /// </summary>
        [Import(typeof(SVsServiceProvider))]
        public IServiceProvider ServiceProvider { get; private set; }

        #region Overrides

        public override void Execute()
        {
            try
            {
                DTE dte = this.ServiceProvider.GetService(typeof(DTE)) as DTE;

                foreach (ProjectItem prjItem in ProjectItems)
                {
                    if ((string.IsNullOrEmpty(Kind) || (prjItem.Kind == Kind)))
                    {
                        string itemPath = DteHelper.BuildPath(prjItem);
                        itemPath = DteHelper.GetPathFull(dte, itemPath);
                        prjItem.Delete();
                        if (File.Exists(itemPath))
                            File.Delete(itemPath);
                    }
                }
            }
            catch { }
        }

        #endregion
    }
}
